<?php

/**
 * 初始化历史好好测数据到好物轻测表
 */
ini_set('display_errors', 1);
error_reporting(-1);
require_once '/data/wwwroot/g_vendor/autoload.php';

use Biz\model\brand\Brand as mBrand;
use Biz\model\GoodsTestActivity\FineGoodsContentModel;
use Biz\model\GoodsTestActivity\FineGoodsModel;
use Biz\model\wiki\Wiki as mWiki;
use Biz\model\wiki\WikiBasic as mWikiBasic;
use Doraemon\model\Evaluation\Evaluation as mEvaluation;
use Doraemon\model\Evaluation\ReportRelateEvaluation as mReportRelateEvaluation;
use Doraemon\service\Evaluation;
use Doraemon\tools\ImgUpload;
use Doraemon\tools\ImgUrl;

changeEvaluationCoverImg();
die;
function changeEvaluationCoverImg()
{
    $evaluationIds = [82, 78, 75, 62, 58, 46];
    foreach ($evaluationIds as $evaluationId) {
        $fineGoodsInfo = FineGoodsModel::get('*', ['type' => FineGoodsModel::TYPE_EVALUATION_OLD, 'evaluation_id' => $evaluationId]);
        if ($fineGoodsInfo && $fineGoodsInfo['basic_id']) {
            $wikiInfo = getWikiInfo($fineGoodsInfo['basic_id']);
            $data = [
                'cover_img' => $wikiInfo ? $wikiInfo['cover_img'] : '',
            ];
            FineGoodsModel::update($data, ['type' => FineGoodsModel::TYPE_EVALUATION_OLD, 'evaluation_id' => $evaluationId, 'id' => $fineGoodsInfo['id']]);
            var_dump(FineGoodsModel::log());
        }
    }
}

$arr = [
    ['85', '灯饰建材', '德国唯宝ViClean-IH | 宝丽洁-IH一体智能座厕，“净”无止境，舒享健康', '238286', '15999', '0'],
    ['84', '家电', '华帝魔尔套系 烟机or灶具', '197111', '3699', '5499'],
    ['83', '灯饰建材', '箭牌卫浴智能马桶AKE1117', '168132', '9999', '0'],
    ['82', '家电', 'A.O史密斯家族年轻时尚品牌 佳尼特零冷水燃气热水器', '170214', '4598', '5798'],
    ['80', '家电', '德国美诺Miele独立式蒸炉&咖啡机组合', '196427', '19999', '0'],
    ['79', '家电', '日立冰箱 整机原装进口 自动制冰 真空保鲜', '130347', '26998', '47800'],
    ['78', '灯饰建材', 'raumplus德禄全屋定制 – 移门/室内门', '153004', '25000', '0'],
    ['77', '灯饰建材', '三星智能锁', '151016', '3480', '0'],
    ['76', '家具', '红苹果创新餐边柜 收纳强大 超强藏线功能', '111001', '4650', '0'],
    ['75', '家电', '福维克可宝VB100无线吸拖擦一体套装，一招搞定地面清洁', '70156', '11178', '0'],
    ['74', '家电', '小吉法式四门复古冰箱 重建科技美学', '137701', '7999', '0'],
    ['73', '家电', '德国美诺Miele 高端智能洗烘套装', '307394', '35316', '0'],
    ['72', '家电', '博世·新净界系列6系洗碗机 洗净烘存·一步到位', '42080', '13590', '0'],
    ['71', '家电', '全新德国美诺Miele G 7310 C SC独立式洗碗机', '192435', '14999', '0'],
    ['69', '灯饰建材', '法恩莎恒温沐氧花洒 细腻柔和 惬意享受', '124872', '3436', '0'],
    ['68', '家电', '洗衣新定义 小吉10+10kg智能洗烘套装', '64559', '15999', '0'],
    ['67', '家电', '方太水槽洗碗机新品E5，洗消烘除存去农残，一体化洁净', '74458', '12500', '0'],
    ['66', '家电', '火星人集成灶 E4B/Z（蒸箱款）', '147501', '9999', '0'],
    ['65', '家电', '科勒维亚和星朗智能座便器', '199299', '20300', '0'],
    ['64', '家电', '倍轻松肩颈按摩器 缓解疼痛疲劳', '70074', '2399', '0'],
    ['63', '日用', '霜山收纳产品组合', '276795', '2000', '0'],
    ['62', '家电', '灵动焕彩 小吉水珠壁挂洗烘一体机', '57991', '3699', '0'],
    ['61', '家电', '方太集成烹饪中心II代，智能黑科技，下厨新体验', '146855', '6900', '27540'],
    ['60', '家电', '大屏护眼，换代之选 海信激光电视', '168003', '11999', '0'],
    ['59', '灯饰建材', '飞利浦智能Wi-Fi LED悦轩吸顶灯', '37877', '1099', '0'],
    ['58', '家电', '能率N7系列13升恒温燃气热水器 恒温舒适洗 多维防干扰', '142790', '3299', '0'],
    ['56', '灯饰建材', '宜家全屋设计 设计理想的家', '166888', '20000', '0'],
    ['54', '家电', '德国美诺Miele 一站式洗烘除菌套装', '138647', '30307.8', '0'],
    ['53', '灯饰建材', '科勒 | 玛亚科感应厨房龙头 “手”到水来 操控自如', '91542', '4000', '4500'],
    ['48', '家具', 'OSIM傲胜 | 经典小天后 / uDiva Classic 按摩沙发', '20937', '14800', '0'],
    ['47', '家电', '极米智能投影极光RS Pro', '192891', '8999', '0'],
    ['46', '家电', '西门子iQ500智护系列洗烘中心 独立洗烘 专业呵护', '101858', '18000', '0'],
    ['45', '家电', '西门子晶蕾®洗碗机 烘干除菌让每一餐都安心', '120600', '13999', '0'],
];
$arrList = [];
$categoryTagIdArr = [
    '家具' => 1,
    '日用' => 2,
    '装饰' => 3,
    '家电' => 4,
    '灯饰建材' => 5,
    '宠物' => 6,
];
$evaluationIds = [];
foreach ($arr as $row) {
    $evaluationIds[] = $row[0];
    $arrList[$row[0]] = [
        'id' => $row[0],
        'category_tag_id' => $categoryTagIdArr[$row[1]] ?? 0,
        'title' => $row[2],
        'basic_id' => $row[3],
        'price' => $row[4],
        'max_price' => $row[5],
    ];
}

$evaluationList = mEvaluation::select('*', ['id' => $evaluationIds]);
//var_dump($evaluationList, array_column($evaluationList, 'id'));
//die;
$countOfOld = Evaluation::getCountOfOld();
foreach ($evaluationList as $row) {
    $fineGoodsId = FineGoodsModel::get('id', ['type' => FineGoodsModel::TYPE_EVALUATION_OLD, 'evaluation_id' => $row['id']]);
    //$title = $row['title'];
//    $replaceArr = ['免费体验｜', '免费申领 | ', '免费申领丨', '免费申领 |', '免费申领 |', '免费申领｜', '免费申领|', '免费申领', '免费领', '免费体验', '免费申请'];
//    $title = str_replace($replaceArr, '', $title);
//    $title = trim($title);
    $title = $arrList[$row['id']]['title'];
    $goodsWorth = $arrList[$row['id']]['price'];
    $goodsMaxWorth = $arrList[$row['id']]['max_price'];
    $categoryTagId = $arrList[$row['id']]['category_tag_id'];
    $basicId = $arrList[$row['id']]['basic_id'];
    $reportList = mReportRelateEvaluation::select('*', ['evaluation_id' => $row['id'], 'status' => mReportRelateEvaluation::STATUS_PUBLISH]);
    $reportNum = count($reportList);
    $wikiInfo = $basicId ? getWikiInfo($basicId) : [];
    $brandUid = $row['brand_uid'];
    $brandInfo = $brandUid ? mBrand::getBrandInfoByUid($brandUid) : [];
    $beautifyNum = $countOfOld[$row['id']] != '' ? $countOfOld[$row['id']] : 0;
    $data = [
        'type' => FineGoodsModel::TYPE_EVALUATION_OLD,
        'activity_title' => $title,
        'brand_id' => $brandInfo ? $brandInfo['id'] : 0,
        'brand_name' => $brandInfo ? $brandInfo['brand_name'] : '',
        'basic_id' => $basicId,
        'basic_title' => $wikiInfo ? $wikiInfo['basic_title'] : '',
        'goods_num' => $row['head_count'],
        'beautify_num' => $beautifyNum,
        'goods_worth' => $goodsWorth,
        'goods_max_worth' => $goodsMaxWorth,
        'schedule_start_time' => $row['start_time'],
        'schedule_end_time' => $row['end_time'],
        'activity_state' => FineGoodsModel::STATE_PASS,
        'report_num' => $reportNum,
        'category_tag_id' => $categoryTagId,
        'cover_img' => $wikiInfo ? $wikiInfo['cover_img'] : '',
        'addtime' => date('Y-m-d H:i:s', $row['addtime']),
        'edittime' => date('Y-m-d H:i:s', $row['edittime']),
        'evaluation_id' => $row['id']
    ];
    if (empty($fineGoodsId)) {
        $fineGoodsId = FineGoodsModel::insertData($data);
    } else {
        FineGoodsModel::updateData($data, ['type' => FineGoodsModel::TYPE_EVALUATION_OLD, 'evaluation_id' => $row['id'], 'id' => $fineGoodsId]);
    }
    if ($fineGoodsId) {
        $contentData = [];
        foreach ($reportList as $report) {
            $contentData = [
                'obj_id' => $report['obj_id'],
                'fine_goods_id' => $fineGoodsId,
                'basic_id' => $basicId,
                'uid' => $report['uid'],
                'addtime' => date('Y-m-d H:i:s', $report['addtime']),
                'edittime' => date('Y-m-d H:i:s', $report['publish_time'])
            ];
            $hasContentId = FineGoodsContentModel::get('id', [
                'obj_id' => $report['obj_id'],
                'fine_goods_id' => $fineGoodsId,
                'basic_id' => $basicId,
                'uid' => $report['uid'],
            ]);
            if (empty($hasContentId)) {
                FineGoodsContentModel::insert($contentData);
            } else {
                FineGoodsContentModel::update($contentData, ['id' => $hasContentId]);
            }
        }
    } else {
        var_dump('导入好物轻测表失败', $row);
    }
}


function getWikiInfo($basicId)
{
    $basicInfo = mWikiBasic::getByWhere(['id' => $basicId], ['id', 'title']);
    $info = mWiki::getByWhere(['basic_id' => $basicId, 'is_default' => 1], ['id', 'title', 'basic_id', 'cover_img', 'is_official', 'is_default', 'status']);
    if ($basicInfo && $info && $info['cover_img']) {
        $url = $info['cover_img'];
        $path = sys_get_temp_dir() . '/' . $basicId . '-' . date('YmdHis') . '.jpg';
        $imgId = ImgUpload::uploadImgsByUrl($url, $path);
        $imgUrl = ImgUrl::getUrlNotSWater(ImgUrl::APPTYPE_BIZ, $imgId, ImgUrl::IMAGE_SIZE_ORI_NPHONE);
        var_dump('上传后到图片id：' . $imgId . '#图片url：' . $imgUrl);
        $info['cover_img'] = $imgId;
        $info['basic_title'] = $basicInfo['title'] ?? $info['title'];
    } else {
        $info = [
            'cover_img' => '',
            'basic_title' => ''
        ];
        var_dump('WIKI ID:' . $basicId . '信息不存在或则图片不存在', $info);
    }
    return $info;
}